package view.rules;

import java.awt.Dimension;
import java.awt.Toolkit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lexicon.LexiconList;
import managers.ComponentManager;
import managers.FeatureManager;
import managers.LexiconManager;
import net.miginfocom.swing.MigLayout;
import javax.swing.JSplitPane;

import features.Feature;

import ontology.PartOfSpeech;

public class AddRulePanel extends JPanel{
	
	private JPanel structAdjRulePanel;
	SpelledOutPanel spelledOutPanel;
	private JPanel featureCopyPanel;
	private JPanel phraseStructurePanel;
	private JPanel menuPanel;
	private JLabel synCatLabel;
	private JComboBox synCatCombo;
	private JLabel groupLabel;
	private JTextField groupTextField;
	private JLabel ruleNameLabel;
	private JTextField ruleNameTextField;
	private JPanel fieldPanel;
	private JSplitPane splitPane;
	private JLabel statusLabel;
	private JCheckBox statusChek;
	private JLabel onLabel;
	private String panelType;

	private RuleTreePanel rulesPanel;
	private LexiconManager lexiconManager;
	private ComponentManager componentManager;
	public AddRulePanel(){
		
		this.panelType = "empty";
		
		componentManager = ComponentManager.getInstance();
		lexiconManager = LexiconManager.getInstance();
		
	
		initComponents();	
		addComponents();
		
	}
	
	public AddRulePanel(String panelType){
		this.panelType = panelType;
		componentManager = ComponentManager.getInstance();
		lexiconManager = LexiconManager.getInstance();	
		initComponents();	
		addComponents();
	}
	
	public String getSelectedCombo(){
		return synCatCombo.getSelectedItem().toString();
	}
	
	private void initComponents(){
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		menuPanel = new JPanel();
		menuPanel.setLayout(new MigLayout());
		menuPanel.setPreferredSize(new Dimension((int)screenSize.getWidth(),(int)screenSize.getHeight()-705));
	
		synCatLabel = new JLabel("Syntactic Category:");
		synCatCombo = new JComboBox(getPOSModel());
		synCatCombo.addActionListener(new BtnGenerateListener());
		synCatCombo.setMinimumSize(new Dimension(150, 20));
		
		groupLabel = new JLabel("Group:");
		groupTextField = new JTextField(100);
		groupTextField.setMinimumSize(new Dimension(700,20));
		groupTextField.setEditable(false);
		
		statusLabel = new JLabel("Status:");
		statusChek = new JCheckBox();
		onLabel = new JLabel("On");
		
		ruleNameLabel = new JLabel("Rule's Name:");
		ruleNameTextField = new JTextField((int)screenSize.getWidth()-50);
		
		fieldPanel = new JPanel();
		fieldPanel.setBorder(BorderFactory.createTitledBorder("Combine"));
		
		rulesPanel = new RuleTreePanel();
	    rulesPanel.setBorder(BorderFactory.createTitledBorder("Rules"));
		
		/* panels for the different rules*/
		structAdjRulePanel = new StructAdjRulePanel();
		spelledOutPanel = new SpelledOutPanel();
		featureCopyPanel = new FeatureCopyPanel();
		phraseStructurePanel = new PhraseStructurePanel();

		/* the third parameter of new JSplitPane, choose any from the above mentioned panels*/
		if(panelType.equals("structAdjRulePanel")){
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, rulesPanel, structAdjRulePanel);
		} else if(panelType.equals("spelledOutPanel")){
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, rulesPanel, spelledOutPanel);
		} else if(panelType.equals("featureCopyPanel")){
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, rulesPanel, featureCopyPanel);
		} else if(panelType.equals("phraseStructurePanel")){
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, rulesPanel, phraseStructurePanel);
		} else if(panelType.equals("empty")){
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, rulesPanel, new JPanel());
		}

		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerSize(10);
		splitPane.setDividerLocation(150);
		splitPane.setPreferredSize(new Dimension((int)screenSize.getWidth(),(int)screenSize.getHeight()- 100));
	}
	
	private void addComponents(){
		addtoMenu();
		add(menuPanel, "wrap");
		
		add(splitPane);
	}
	
	private void updatePOS(LexiconList list){
		
	}
	private DefaultComboBoxModel getPOSModel(){
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel();
		ArrayList<PartOfSpeech> list = componentManager.getPartsOfSpeech();
		for(PartOfSpeech p: list){
			model.addElement(p.getName());
		}
		
		return model;
		
	}
	private void addtoMenu(){
		
		menuPanel.add(synCatLabel);
		menuPanel.add(synCatCombo);
		menuPanel.add(groupLabel);
		menuPanel.add(groupTextField);
		menuPanel.add(statusLabel, "gapleft 20");
		menuPanel.add(statusChek);
		menuPanel.add(onLabel, "wrap");
		menuPanel.add(ruleNameLabel);
		menuPanel.add(ruleNameTextField, "span");
		
	}
	
	class BtnGenerateListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			System.out.println(synCatCombo.getSelectedItem().toString());
			LexiconList list = lexiconManager.getLexiconList("adj");
			ArrayList<Feature> featureList = FeatureManager.getDefaultFeatures(synCatCombo.getSelectedItem().toString());
			System.out.println(list == null);
			SpelledOutPanel p = (SpelledOutPanel) spelledOutPanel;
	
				p.setLexiconList(list);
			
			
			p.setFeatureList(synCatCombo.getSelectedItem().toString(),ruleNameTextField.getText());
	
		}

	}

}
